{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numpy的用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. stack()，hstack()，vstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=[[1,2,3],\n",
    "   [4,5,6]]\n",
    "b=[[1,2,3],\n",
    "   [4,5,6]]\n",
    "c=[[1,2,3],\n",
    "   [4,5,6]]\n",
    "\n",
    "print(\"a=\",a)\n",
    "print(\"b=\",b)\n",
    "print(\"c=\",c)\n",
    "\n",
    "d=np.stack((a,b,c),axis=0)\n",
    "print('\"axis=0\":\\n',d, '\\n d.shape:', d.shape)\n",
    "\n",
    "d=np.stack((a,b,c),axis=1)\n",
    "print('\"axis=1\":\\n',d, '\\n d.shape:', d.shape)\n",
    "\n",
    "d=np.stack((a,b,c),axis=2)\n",
    "print('\"axis=2\":\\n',d, '\\n d.shape:', d.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array((1,2,3))\n",
    "b = np.array((2,3,4))\n",
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1],[2],[3]])\n",
    "b = np.array([[2],[3],[4]])\n",
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=[[1,2,3],\n",
    "   [4,5,6]]\n",
    "b=[[1,2,3],\n",
    "   [4,5,6]]\n",
    "c=[[1,2,3],\n",
    "   [4,5,6]]\n",
    "print(np.hstack((a,b,c)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1, 2, 3])\n",
    "b = np.array([2, 3, 4])\n",
    "np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "删除指定元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.delete(a, -1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数组大小判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1], [2], [3]])\n",
    "b = np.array([[2], [3], [4]])\n",
    "\n",
    "print(np.vstack((a,b)))\n",
    "print(np.vstack((a,b)).T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=[[1,2,3],\n",
    "   [4,5,6]]\n",
    "b=[[1,2,3],\n",
    "   [4,5,6]]\n",
    "c=[[1,2,3],\n",
    "   [4,5,6]]\n",
    "print(np.vstack((a,b,c)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy.apply_along_axis(func, axis, arr, *args, **kwargs)：\n",
    "\n",
    "必选参数：func,axis,arr。其中func是我们自定义的一个函数，函数func(arr)中的arr是一个数组，函数的主要功能就是对数组里的每一个元素进行变换，得到目标的结果。\n",
    "\n",
    "                 其中axis表示函数func对数组arr作用的轴。\n",
    "\n",
    "可选参数：*args, **kwargs。都是func()函数额外的参数。\n",
    "\n",
    "返回值：numpy.apply_along_axis()函数返回的是一个根据func()函数以及维度axis运算后得到的的数组."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_func(a):\n",
    "    return  (a[0] + a[-1]) * 0.5\n",
    "\n",
    "b=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])\n",
    "\n",
    "b[0]+b[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(np.apply_along_axis(my_func, 0, b))\n",
    "print(np.apply_along_axis(my_func, 1, b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.signbit - 返回设置有符号位(小于零)的元素级True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(np.signbit(-1.2))\n",
    "print(np.signbit(np.array([1, -2.3, 2.1])))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy.where() 有两种用法：\n",
    "\n",
    "1. np.where(condition, x, y)\n",
    "\n",
    "满足条件(condition)，输出x，不满足输出y。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aa = np.arange(10)\n",
    "np.where(aa,1,-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.corrcoef()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Array1 = [[1, 2, 3], [4, 5, 6]]\n",
    "Array2 = [[11, 25, 346], [734, 48, 49]]\n",
    "Mat1 = np.array(Array1)\n",
    "Mat2 = np.array(Array2)\n",
    "correlation = np.corrcoef(Mat1, Mat2)\n",
    "print(\"矩阵1=\\n\", Mat1)\n",
    "print(\"矩阵2=\\n\", Mat2)\n",
    "print(\"相关系数矩阵=\\n\", correlation)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.dot() 返回的是两个数组的点积(dot product)\n",
    "\n",
    "什么是点积https://baike.baidu.com/item/%E7%82%B9%E7%A7%AF/9648528?fr=aladdin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一维数组之间的点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = np.arange(0,9)\n",
    "e = d[::-1]\n",
    "np.dot(d,e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "二维数组（矩阵） 之间的点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(1,5).reshape(2,2)\n",
    "b = np.arange(5,9).reshape(2,2)\n",
    "np.dot(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.argmin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = np.arange(6).reshape(2,3)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.argmin(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.argmin(np.dot(a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.argmin(c, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.argmin(c, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.linspace主要用来创建等差数列\n",
    "\n",
    "https://blog.csdn.net/Asher117/article/details/87855493"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.linspace(1, 5, 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.column_stack, row_stack https://www.cnblogs.com/shuangcao/p/11380964.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [[1,2,7],\n",
    "     [-6,-2,-3],\n",
    "     [-4,-8,-55]\n",
    "     ]\n",
    "b = [3,5,6]\n",
    "a = np.array(a)\n",
    "b = np.array(b)\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_b_column = np.column_stack((a,b))#左右根据列拼接\n",
    "a_b_row = np.row_stack((a,b))#上下按照行拼接\n",
    "print('a_b_column')\n",
    "print(a_b_column)\n",
    "print('a_b_row')\n",
    "print(a_b_row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.polyfit() 进行曲线拟合，通常是结合poly1d函数一起使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=[ 1 ,2  ,3 ,4 ,5 ,6]\n",
    "Y=[ 2.5 ,3.51 ,4.45 ,5.52 ,6.47 ,7.51]\n",
    "z1 = np.polyfit(X, Y, 1)  #一次多项式拟合，相当于线性拟合\n",
    "p1 = np.poly1d(z1)\n",
    "print(z1)  #[ 1.          1.49333333]\n",
    "print(p1)  # 1 x + 1.493"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 大矩阵运算中的dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bmarr = np.random.rand(2000000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.347483368590474"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.ndarray((42393, 42393), dtype=np.float16)\n",
    "a.nbytes/1024**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "true_similarity_matrix = np.random.randint(1,20,size=(10000,10000))\n",
    "true_similarity_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "similarity_matrix = np.random.rand(10000, 10000)\n",
    "similarity_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 代码段 1，true_similarity_matrix 是 int, similarity_matrix 是 float\n",
    "tmp_matrix = similarity_matrix * true_similarity_matrix   # 内存会炸掉，两个 10000*10000 维 float array\n",
    "num_correct_edge = sum(sum(tmp_matrix))\n",
    "\n",
    "# # 代码段 2\n",
    "# for i in range():\n",
    "#     for j in range():\n",
    "#         set_true_ij.append(i,j)\n",
    "# num_correct_edge = 0\n",
    "# for i, j in set_true_ij:\n",
    "#     num_correct_edge += similarity_matrix[i,j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
